package com.tianyadu.security.core.validate.code;

import com.tianyadu.security.core.properties.propertieVO.SecurityConstants;
import com.tianyadu.security.core.validate.code.service.ValidateCodeProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @FileName: ValidateCodeController
 * @Description: 生成校验码的请求处理器
 * @author: xueyj
 * @create: 2018-12-11 06:57
 */
@RestController
public class ValidateCodeController {

	/**
	 * spring依赖搜索
	 * 收集系统中所有的 {@link ValidateCodeProcessor} 接口的实现。
	 * 当spring容器启动时，会自动查找ValidateCodeGenerator接口下的实现，将相应接口实现Bean名称放入map中的key
	 */
	@Autowired
	private Map<String, ValidateCodeProcessor> validateCodeProcessors;

	/**
	 * 创建验证码，根据验证码类型不同，调用不同的 {@link ValidateCodeProcessor}接口实现
	 * @param request
	 * @param response
	 * @param type
	 * @throws Exception
	 */
	@GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{type}")
	public void createCode(HttpServletRequest request, HttpServletResponse response,
						   @PathVariable String type) throws Exception {
		validateCodeProcessors.get(type+"ValidateCodeProcessor").create(new ServletWebRequest(request, response));
	}

}
